package com.wdy.action;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.wdy.po.Repair;
import com.wdy.po.Tenement;
import com.wdy.po.User;
import com.wdy.service.RepairManager;
import com.wdy.vo.Condition;
import com.wdy.vo.Page;
import com.wdy.vo.Result;

@SuppressWarnings( { "serial", "unused" })
public class RepairAction extends BasicAction {
	private static final Log log = LogFactory.getLog(RepairAction.class);
	private Repair repair;
	private RepairManager repairManager;
	private Page page;
	private List<Condition> conList;
	private String[] ids;
	private Map<String, Object> session;
	private HttpServletResponse response;
	private HttpServletRequest request;
	private ServletContext servletContext;

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	public Repair getRepair() {
		return repair;
	}

	public void setRepair(Repair repair) {
		this.repair = repair;
	}

	public void setRepairManager(RepairManager repairManager) {
		this.repairManager = repairManager;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<Condition> getConList() {
		return conList;
	}

	public void setConList(List<Condition> conList) {
		this.conList = conList;
	}

	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	// repair list.action
	@SkipValidation
	public String listAll() {
		List<Repair> list = null;
		list = (List<Repair>) repairManager.listAll();
		if (null != list) {
			request.setAttribute("repairList", list);
		}
		return SUCCESS;
	}

	// repair add.action
	public String add() {
		repairManager.saveRepair(repair);
		return SUCCESS;
	}

	// repair delete.action
	@SkipValidation
	public String delete() {
		repairManager.removeRepair(repair);
		return SUCCESS;
	}

	// repair update.action
	public String update() {
		repairManager.updateRepair(repair);
		return SUCCESS;
	}

	// repair findByExample.action
	@SkipValidation
	public String find() {
		List<Repair> list = null;
		list = (List<Repair>) repairManager.findByExample(repair);
		if (null != list) {
			request.setAttribute("repairs", list);
		}
		return SUCCESS;
	}

	// repair findTenementByRid.action
	@SkipValidation
	public String findTenement() {
		List<Tenement> list = null;
		list = (List<Tenement>) repairManager.findTenement(repair.getRid());
		if (null != list) {
			request.setAttribute("tenements", list);
		}
		return SUCCESS;
	}

	// repair get.action
	@SkipValidation
	public String get() {
		Repair repair = null;
		repair = (Repair) repairManager.getRepair(this.repair.getRid());
		if (null != repair) {
			request.setAttribute("repair", repair);
		}
		return SUCCESS;
	}

	// repair change.action
	@SkipValidation
	public String change() throws Exception {
		Repair repair = null;
		repair = (Repair) repairManager.getRepair(this.repair.getRid());
		if (null != repair) {
			request.setAttribute("repair", repair);
		}
		return SUCCESS;
	}

	// repair query_repair.action
	@SkipValidation
	public String query() throws Exception {
		// conList = (List<Condition>)session.get("conList");
		if (null == conList || conList.size() == 0) {
			conList = new LinkedList<Condition>();
		}
		Iterator<Condition> it = conList.iterator();
		while (it.hasNext()) {
			Condition con = (Condition) it.next();
			if (null != con.getValues()) {
				Object[] values = con.getValues();
				if (null == values[0] || "".equals(values[0])) {
					it.remove();
				}
			} else {
				it.remove();
			}
		}

		if (null == page) {
			page = new Page();
			page.setEveryPage(5);
			page.setCurrentPage(1);
		}
		Result result = repairManager.query(conList, page);
		request.setAttribute("page", result.getPage());
		request.setAttribute("repairList", result.getContent());
		// session.put("conList", conList);
		return SUCCESS;
	}

	// repair delete_batch.action
	@SkipValidation
	public String deleteBatch() throws Exception {
		if (null != ids) {
			Integer[] id = new Integer[ids.length];
			for (int i = 0; i < ids.length; i++) {
				id[i] = new Integer(ids[i]);
			}
			repairManager.removeRepairs(id);
		}
		return SUCCESS;
	}
}
